# Include python
find_package(Python COMPONENTS Interpreter Development)
include_directories(${Python_INCLUDE_DIRS})

# Include headers
include_directories(../../include ../../xcomm/include)
set(SWIG_OUTFILE_DIR ${CMAKE_CURRENT_BINARY_DIR}/_pyswig)

# Add swig module
set_property(SOURCE python.i PROPERTY CPLUSPLUS ON)

swig_add_library(xovip LANGUAGE python SOURCES 
python.i
../../src/comm/axi4_async.cpp
../../src/comm/axi4.cpp
../../src/comm/mem.cpp
)

# set buld output directory
set(OVIP_LIB_DIR ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/../python/ovip)
set_target_properties(xovip PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${SWIG_OUTFILE_DIR})

add_library(xspcomm_so SHARED IMPORTED)
set_target_properties(xspcomm_so PROPERTIES
    IMPORTED_LOCATION ${CMAKE_CURRENT_BINARY_DIR}/../../xcomm/lib/libxspcomm.so
)
add_dependencies(xovip xspcomm_so)
target_link_libraries(xovip xspcomm_so)

# copy file
set_source_files_properties(${CMAKE_CURRENT_BINARY_DIR}/xovip.py PROPERTIES GENERATED TRUE)
add_custom_command(
    OUTPUT  ${OVIP_LIB_DIR}/__init__.py
    COMMAND ${CMAKE_COMMAND} -E copy
            ${SWIG_OUTFILE_DIR}/_xovip.so
            ${OVIP_LIB_DIR}/_xovip.so.${PROJECT_VERSION}
    COMMAND ${CMAKE_COMMAND} -E copy
            ${CMAKE_CURRENT_BINARY_DIR}/xovip.py
            ${OVIP_LIB_DIR}/xovip.py
    COMMAND ${CMAKE_COMMAND} -E copy
            ${CMAKE_CURRENT_SOURCE_DIR}/ovip.py
            ${OVIP_LIB_DIR}/__init__.py
    DEPENDS xovip
)

add_custom_command(
    OUTPUT ovip_so_lnk
    COMMAND ${CMAKE_COMMAND} -E create_symlink
            "_xovip.so.${PROJECT_VERSION}"
            "_xovip.so"
    WORKING_DIRECTORY ${OVIP_LIB_DIR}
    DEPENDS  ${OVIP_LIB_DIR}/__init__.py
)

add_custom_target(
  _DummyTarget_create_ovip_link ALL
  DEPENDS ovip_so_lnk
)

install(DIRECTORY ${OVIP_LIB_DIR} DESTINATION . USE_SOURCE_PERMISSIONS)
